#
# @lc app=leetcode.cn id=476 lang=python
#
# [476] 数字的补数
#

# @lc code=start
class Solution(object):
    def findComplement(self, num):
        """
        :type num: int
        :rtype: int
        """
        # return int(''.join(['1' if i == '0' else '0' for i in bin(num)[2:]]), 2)
        result=0
        position=0
        while num:
            result+=(num+1)%2*(2**(position))
            num//=2
            position+=1
        return result
# @lc code=end

